package tienmanh.gs.goldminer.sprite.shop;

import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.PathModifier;
import org.andengine.entity.modifier.ScaleAtModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.primitive.Line;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.math.MathUtils;
import org.andengine.util.modifier.IModifier;

import tienmanh.gs.goldminer.scene.GameScene;
import tienmanh.gs.goldminer.sprite.Hook;
import tienmanh.gs.goldminer.sprite.MyText;

public class Clock extends AnimatedSprite implements ITouchArea{

	Line line;
	Hook hook;
	public Clock(float pX, float pY,float w,float h,
			ITiledTextureRegion pTiledTextureRegion,
			VertexBufferObjectManager pVertexBufferObjectManager, Line line,Hook hook) {
		super(pX, pY,w,h, pTiledTextureRegion, pVertexBufferObjectManager);
		// TODO Auto-generated constructor stub
		this.line = line;
	    this.hook = hook;
	}
	public void moveText( float paramFloat3, float paramFloat4 , final int value)
	  {
	    final SequenceEntityModifier entityModifier = new SequenceEntityModifier(
				new IEntityModifierListener() {
					@Override
					public void onModifierStarted(
							final IModifier<IEntity> pModifier,
							final IEntity pItem) {
					}
	//
					@Override
					public void onModifierFinished(
							final IModifier<IEntity> pEntityModifier,
							final IEntity pEntity) {
							GameScene.sceneHUD.detachChild(GameScene.arrayGolds.get(value));
//							GameScene.arrayGolds.remove(value);
							GameScene.arrayGolds.set(value, null);
						    GameScene.sceneHUD.detachChild(Clock.this);
						    GameScene.getInstace().isPull = false;
						    hook.setVisible(true);
						    hook.unPullHook();
							hook.Rotate();
							GameScene.miner.stopAnimation();
//							GameScene.hook.updatePosition(MathUtils.random(2, 175));
							if(GameScene.isMinerRunning)
								GameScene.miner.MinerRun();
							GameScene.pullGold = false;
							GameScene.getInstace().isTouch = true;
							GameScene.getInstace().waitingThrowHook(hook);
							GameScene.times++;
							GameScene.item.writeItemSP3(GameScene.times);
							GameScene.tvCountTime.setText(""+GameScene.times);
					}
				}, new PathModifier(1.0F, new PathModifier.Path(3).to(getX(), getY()).to(300, paramFloat4).to(paramFloat3, paramFloat4)));
		    registerEntityModifier(entityModifier);
	  }
	@Override
	public boolean onAreaTouched(TouchEvent arg1,
			float pTouchAreaLocalX, float pTouchAreaLocalY) {
		// TODO Auto-generated method stub
		if(GameScene.times > 0){
			GameScene.getInstace().time+=15;
			if(GameScene.getInstace().time > 60 ) GameScene.getInstace().time=60;
			GameScene.getInstace().tvTime.registerEntityModifier(new SequenceEntityModifier(
					new IEntityModifierListener() {
						@Override
						public void onModifierStarted(
								final IModifier<IEntity> pModifier,
								final IEntity pItem) {
						}
		//
						@Override
						public void onModifierFinished(
								final IModifier<IEntity> pEntityModifier,
								final IEntity pEntity) {
							GameScene.getInstace().tvTime.registerEntityModifier(new ScaleAtModifier(1, 2, 1, 0, 0));
						}
					},new ScaleAtModifier(1, 1f, 2, 0, 0)));
			GameScene.times--;
			GameScene.item.writeItemSP3(GameScene.times);
			GameScene.tvCountTime.setText(""+GameScene.times);
		}
		return super
				.onAreaTouched(arg1, pTouchAreaLocalX, pTouchAreaLocalY);
	}

}
